package com.lovo.roomProject.classManager.dao.impl;

import com.lovo.roomProject.classManager.dao.ClassDAO;
import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.ClaszDO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.StudentDO;
import com.lovo.roomProject.student.dao.impl.StudentDAOImpl;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ClassDAOImpl extends BaseDAO implements ClassDAO {
    private ClaszDO fillClasz() throws SQLException{
        ClaszDO claszDO = new ClaszDO();
        claszDO.setId(rs.getInt("c_id"));
        claszDO.setName(rs.getString("c_name"));
        claszDO.setTeacher(rs.getString("c_teacher"));
        claszDO.setCreateDate(LocalDate.parse(rs.getString("c_create_date")));
        return claszDO;
    }
    @Override
    public List listClass() {
       this.getConnection();
       String sql = "select * from t_class";
       List list = new ArrayList();
        try {
            statement= con.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()){
                ClaszDO claszDO = fillClasz();
                list.add(claszDO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeConnection();
        }
        return list;
    }

    @Override
    public CutPageBean listClassByPage(int current) {
        this.getConnection();
        String selectSql = "select * ";
        String countSql = "select count(*) cnt ";
        String fromSql = "from t_class ";
        String limitSql = "limit ?,?";
        CutPageBean cutPageBean = new CutPageBean();
        List list = new ArrayList();
        try {
            statement = con.prepareStatement(selectSql+fromSql+limitSql);
            statement.setObject(1,(current-1)*CutPageBean.PAGESIZE);
            statement.setObject(2,CutPageBean.PAGESIZE);
            rs = statement.executeQuery();
            while (rs.next()) {
                ClaszDO claszDO = fillClasz();
                list.add(claszDO);
            }
            statement = con.prepareStatement(countSql+fromSql);
            rs = statement.executeQuery();
            rs.next();
            cutPageBean.setCount(rs.getInt("cnt"));
            cutPageBean.setList(list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeConnection();
        }
        return cutPageBean;
    }

    @Override
    public ClaszDO getClassByName(String name) {
        this.getConnection();
        String sql = "select * from t_class where c_name=?";
        ClaszDO claszDO = new ClaszDO();
        try {
            statement= con.prepareStatement(sql);
            statement.setObject(1,name);
            rs = statement.executeQuery();
            if (rs.next()){
                claszDO=fillClasz();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeConnection();
        }
        return claszDO;
    }

    @Override
    public ClaszDO getClassById(int id) {
        this.getConnection();
        String sql = "select * from t_class where c_id=?";
        ClaszDO claszDO = new ClaszDO();
        try {
            statement= con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if (rs.next()){
                claszDO=fillClasz();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeConnection();
        }
        return claszDO;
    }

    @Override
    public void insert(ClaszDO claszDO) {
        this.getConnection();
        String sql = "insert into t_class(c_name,c_create_date,c_teacher) " +
                "value (?,?,?)";
        try {
            statement= con.prepareStatement(sql);
            statement.setObject(1,claszDO.getName());
            statement.setObject(2,claszDO.getCreateDate().toString());
            statement.setObject(3,claszDO.getTeacher());
            statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeConnection();
        }
    }


    @Override
    public void delete(int id) {
        this.getConnection();
        String sql = "delete from t_class where c_id=?";
        try {
            statement= con.prepareStatement(sql);
            statement.setObject(1,id);
            statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeConnection();
        }
    }
}
